
public class KohonenNeuron extends LinearNeuron {

	public KohonenNeuron(int weightsNum) {
		super(weightsNum);
	}
	
	public float countDistance(NeuronLayer input) {
		float distance = 0;
		for(int i=0;i<weights.size();i++) {
			distance += Math.abs(weights.get(i)-input.getNeurons().get(i).getOutput());
		}
		return distance;
	}
	
	public static float countNeighbourhood(int i, int j, int matrixRowSize) {
		float neighbourhood = 0;
		if(i==j) return 1;
		switch(neighbourhoodDimension) {
			case 1:
				neighbourhood = Math.max(0.0f,(float)(neighbourhoodRadius-Math.abs(i-j))/neighbourhoodRadius);
				break;
			case 2:
				int distance = (int) (Math.abs(i-j)%matrixRowSize+Math.floor(Math.abs(i-j)/matrixRowSize));
				neighbourhood = Math.max(0.0f,(float)(neighbourhoodRadius-distance)/neighbourhoodRadius);
				break;
			default:
				return 0;
		}
		return neighbourhood;
	}

	public void learn(NeuronLayer input, Float neighbourhood) {
		Float tmp;
		if(neighbourhood>0) {
			for(int i=0;i<weights.size();i++) {
				tmp = weights.get(i);
				System.out.println("neuron learning: alpha: "+alpha+" neighbourhooh: "+neighbourhood+" diff: "+(input.getNeurons().get(i).getOutput()-tmp));
				setWeight(i,tmp+alpha*neighbourhood*(input.getNeurons().get(i).getOutput()-tmp));
			}
		}
	}

}
